<?php
// 本类由系统自动生成，仅供测试用途
class IndexAction extends BaseAction {

    public static $indexUrl = "index/id/#1#";
    public function index(){
    	// C('LAYOUT_ON',false);
        $code = $this->_get('code');
        // echo $code;
        //传过来的商品码
        $this->assign('code',trim($code));
    	$this->display();
    }

    public function detail()
    {
        // C('LAYOUT_ON',false);
        $code = $this->_get('code');

        $illege = false;
        if($code)
        {
            $good = D('Goods')->getGoodsByCode($code);
            //商品存在且没有被领取
            if($good && $good['status'] && $good['deadline']>time())
            {
                $this->assign('data',$good);
                $this->assign('code',$code);
                $this->display('detail');
            }else{
                //2B要跳转啊
                redirect(U('Home/Index/index',array('code'=>$code)));
            }
        }else{
            $msg = '不存在该奖品';
            $illege = true;
        }
        if($illege)
        {
            $this->assign('msg',$msg);
            $this->display('error');
        }
        
    }

    public function apply()
    {
        $username = $this->_post('username');
        $phone = $this->_post('phone');
        $sncode = $this->_post('code');

        $location_p = $this->_post('location_p');
        $location_c = $this->_post('location_c');
        $location_a = $this->_post('location_a');

        $addr = $this->_post('addr');
        $remark = $this->_post('remark');

        $re = D('Goods')->checkGoodsValid($sncode);
        //判断奖品状态
        if($re['status']==0)
        {
            echo returnJson('400',array(),'该奖品已被领取');exit;
        }
        if($re['deadline']<time())
        {
            echo returnJson('400',array(),'该奖品已过期');exit;
        }

        if($username && $phone && $sncode && $location_p && $location_c && $location_a && $addr)
        {
            $data = array();
            $data['username'] = $username;
            $data['phone'] = $phone;
            $data['sncode'] = $sncode;
            $data['area'] = implode(' ', array($location_p,$location_c,$location_a));
            $data['address'] = $addr;
            $data['remark'] = $remark;
            $data['create_time'] = time();

            $instId = M('prize')->add($data);

            if($instId){
                $re = D('Goods')->setGoodsUnable($sncode);
                if($re)
                {
                    $ret = returnJson('200',array(),'提交成功');
                }else{
                    $ret = returnJson('400',array(),'提交失败');
                }
            }else{
                $ret = returnJson('400',array(),'提交失败');
            }
        }else
        {
            /*$this->assign('msg','填写信息不完整');
            $this->display('error');*/
            $ret = returnJson('400',array(),'填写信息不完整');
        }
    
        echo $ret;exit;
    }

    public function succ()
    {
        /*$refer = $_SERVER['HTTP_REFFERT'];
        $referhost = parse_url($refer);
        if()
        {
            $this->display();
        }else{
            $this->assign('msg','请至江苏联通微信服务号领取您的专属礼物');
        }*/
        $this->display();
        
    }

    public function checkGoods()
    {

        $sncode = $this->_post('code');
        // echo $sncode;exit;
        $re = D('Goods')->checkGoodsValid($sncode);

        if($re['status'])
        {
            if($re['deadline']>time()) //有效期以内
            {
                $ret = returnJson('200',array(),'有效');
            }else
            {
                $ret = returnJson('401',array(),'奖品已过期');
            }
        }else{
            $ret = returnJson('400',array(),'奖品已被领取');
        }
        echo $ret;exit;
    }

    public function test(){
        $str_chr = chr(rand(97,122));
        $str_num = mt_rand(10000, 99999);
        $uniqid = md5(uniqid());
        $unq_compile = substr($uniqid, 0,6) . substr($uniqid, -6);
 
        echo $str_chr.$unq_compile.$str_num;exit;
    }
}